package com.lvyp.server.controller;


import com.lvyp.server.pojo.Admin;
import com.lvyp.server.pojo.RespBean;
import com.lvyp.server.pojo.Role;
import com.lvyp.server.service.IAdminService;
import com.lvyp.server.service.IRoleService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  用户控制器
 * </p>
 *
 * @author lvyp
 * @since 2021-04-24
 */
@RestController
@RequestMapping("/system/admin")
public class AdminController {
    @Autowired
    private IAdminService adminService;
    @Autowired
    private IRoleService roleService;

    @ApiOperation(value = "获取所有用户")
    @GetMapping("/")
    public List<Admin> getAllAdmins(String keywords) {
        return adminService.getAllAdmins(keywords);
    }

    @ApiOperation(value = "更新用户")
    @PutMapping("/")
    public RespBean updateAdmin(@RequestBody Admin admin) {
        if (adminService.updateById(admin)) {
            return RespBean.success("更新成功");
        }
        return RespBean.error("更新失败");
    }

    @ApiOperation(value = "删除用户")
    @DeleteMapping("/{id}")
    public RespBean deleteAdminById(@PathVariable("id") Integer id){
        if (adminService.removeById(id)){
            return RespBean.success("删除成功");
        }
        return RespBean.error("删除失败");
    }

    /*----------------操作用户的角色------------------*/

    @ApiOperation(value = "查询所有角色")
    @GetMapping("/roles")
    private List<Role> getAllRoles(){
        return roleService.list();
    }

    /**
     *
     * @param adminId
     * @param rids
     * @return
     */
    @ApiOperation(value = "更新用户的角色")
    @PutMapping("/role")
    private RespBean updateRole(@RequestParam Integer adminId,@RequestParam Integer[] rids){
        return adminService.updateAdminRole(adminId,rids);
    }

}
